package com.becca.cake.controller.system;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.becca.cake.core.common.BaseAction;
import com.becca.cake.core.common.PagingBean;
import com.becca.cake.model.system.AdminDO;
import com.becca.cake.model.system.AdminSubscriberSettingDO;
import com.becca.cake.service.system.IAdminsService;
import com.becca.cake.util.JsonUtil;
import com.becca.cake.util.StringUtil;

/**
 * 员工管理
 * @author hejinguo
 * @version $Id: AdminsAction.java, v 0.1 2013-12-2 上午03:27:13
 */
@Controller
@RequestMapping("/systemAdmin")
public class AdminsAction extends BaseAction {
    private final static Logger logger = LoggerFactory.getLogger(AdminsAction.class);
    @Autowired
    private IAdminsService      iAdminsService;

    /**
     * 查询员工信息
     * @author hejinguo
     * @param adminDO  注意,页面中name只要对应实体的属性spring会将数据封装到实体中
     * @return
     */
    @RequestMapping(value = "/selectAllAdmin", method = RequestMethod.POST)
    public @ResponseBody
    String selectAllAdmin(AdminDO adminDO) {
        PagingBean page = getInitPagingBean();
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("priAdminDO", adminDO);
        map.put("pagingBean", page);
        map.put("postId", this.getUserSeesion().getPostId());
        map.put("adminId", this.getUserSeesion().getId());
        //查询员工信息
        try {
            List<AdminDO> listAdmin = this.iAdminsService.selectAllAdmin(map);
            Long count = this.iAdminsService.selectAllAdminCount(map);
            return JsonUtil.writeObjectPageJSON(listAdmin, count);
        } catch (Exception e) {
            logger.error("查询员工信息失败!", e);
            return JsonUtil.returnJsonUnSuccess("查询员工信息失败!");
        }
    }

    /**
     * 根据角色ID查询该角色对应的人员
     * @author hejinguo
     * @param isUnable
     * @return
     */
    @RequestMapping(value = "/selectPersonnelByRoleId", method = RequestMethod.POST)
    public @ResponseBody
    String selectPersonnelByRoleId(@RequestParam(value = "roleId", required = false) String roleId) {
        if (StringUtil.isNotEmpty(roleId)) {
            try {
                PagingBean page = getInitPagingBean();
                Map<String, Object> map = new HashMap<String, Object>();
                map.put("pagingBean", page);
                map.put("roleId", roleId);
                List<AdminDO> listAdmin = this.iAdminsService.selectPersonnelByRoleId(map);
                Long count = this.iAdminsService.selectPersonnelByRoleIdCount(map);
                return JsonUtil.writeObjectPageJSON(listAdmin, count);
            } catch (Exception e) {
                logger.error("根据角色ID查询该角色对应的人员失败!", e);
                return JsonUtil.returnJsonUnSuccess("根据角色ID查询该角色对应的人员失败!");
            }
        } else {
            logger.equals("根据角色ID查询该角色对应的人员时角色ID为空!");
            return JsonUtil.returnJsonUnSuccess("根据角色ID查询该角色对应的人员时角色ID为空!");
        }
    }

    /**
     * 添加或修改员工信息
     * @author hejinguo
     * @return
     */
    @RequestMapping(value = "/insertOrUpdateAdmin", method = RequestMethod.POST)
    public @ResponseBody
    String insertOrUpdateAdmin(@ModelAttribute AdminDO adminDO) {
        if (adminDO != null) {
            try {
                adminDO.setCreatedMan(this.getUserSeesion().getTrueName());
                adminDO.setUpdatedMan(this.getUserSeesion().getTrueName());
                this.iAdminsService.insertOrUpdateAdmin(adminDO);
                return JsonUtil.returnJsonSuccess("添加或修改员工信息成功!");
            } catch (Exception e) {
                logger.error("添加或修改员工信息失败!", e);
                return JsonUtil.returnJsonUnSuccess("添加或修改员工信息失败!");
            }
        } else {
            logger.error("添加或修改员工信息为空!");
            return JsonUtil.returnJsonUnSuccess("添加或修改员工信息为空!");
        }
    }

    /**
     * 根据ID查询员工信息
     * @author hejinguo
     * @param adminId
     * @return
     */
    @RequestMapping(value = "/selectAdminById", method = RequestMethod.POST)
    public @ResponseBody
    String selectAdminById(@RequestParam(value = "adminId", required = false) String adminId) {
        if (StringUtil.isNotEmpty(adminId)) {
            try {
                return this.iAdminsService.selectAdminById(adminId);
            } catch (Exception e) {
                logger.error("查询员工信息时失败!");
                return JsonUtil.returnJsonUnSuccess("查询员工信息时失败!");
            }
        } else {
            logger.error("查询员工信息时ID为空!");
            return JsonUtil.returnJsonUnSuccess("查询员工信息时ID为空!");
        }
    }

    /**
     * 查询当前员工名称是否存在
     * @author hejinguo
     * @param adminName
     * @return
     */
    @RequestMapping(value = "/selectAdminNameExist", method = RequestMethod.POST)
    public @ResponseBody
    String selectAdminNameExist(@RequestParam(value = "adminName", required = false) String adminName) {
        if (StringUtil.isNotEmpty(adminName)) {
            try {
                return this.iAdminsService.selectAdminNameExist(adminName);
            } catch (Exception e) {
                logger.error("查询当前员工名称是否存在异常!", e);
                return JsonUtil.returnJsonUnSuccess("查询当前员工名称是否存在异常!");
            }
        } else {
            logger.error("员工名称不能为空!");
            return JsonUtil.returnJsonUnSuccess("员工名称不能为空!");
        }
    }

    /**
     * 根据功能ID查询对应人员信息
     * @author hejinguo
     * @param functionId
     * @return
     */
    @RequestMapping(value = "/selectAdminByFunctionId", method = RequestMethod.POST)
    public @ResponseBody
    String selectAdminByFunctionId(@RequestParam(value = "functionId", required = false) String functionId) {
        if (StringUtil.isNotEmpty(functionId)) {
            try {
                return this.iAdminsService.selectAdminByFunctionId(functionId);
            } catch (Exception e) {
                logger.error("根据功能ID查询对应人员信息异常!", e);
                return JsonUtil.returnJsonUnSuccess("根据功能ID查询对应人员信息异常!");
            }
        } else {
            logger.error("根据功能ID查询对应人员信息功能ID不能为空!");
            return JsonUtil.returnJsonUnSuccess("根据功能ID查询对应人员信息功能ID不能为空!");
        }
    }

    /**
     * 设置是否添加提醒
     * @param isSubscription
     * @return
     */
    @RequestMapping(value = "/updatePriAdminSubscription", method = RequestMethod.POST)
    public @ResponseBody
    String updatePriAdminSubscription(@RequestParam(value = "isSubscription", required = false) Byte isSubscription) {
        if (isSubscription != null) {
            try {
                Map<String, Object> map = new HashMap<String, Object>();
                map.put("id", this.getUserSeesion().getId());
                map.put("isSubscription", isSubscription);
                this.iAdminsService.updatePriAdminSubscription(map);
                this.getUserSeesion().setIsSubscription(isSubscription);
                return JsonUtil.returnJsonSuccess("");
            } catch (Exception e) {
                logger.error("设置是否提醒异常!", e);
                return JsonUtil.returnJsonUnSuccess("设置是否提醒异常!");
            }
        } else {
            logger.error("设置提醒类型不能为空!");
            return JsonUtil.returnJsonUnSuccess("设置提醒类型不能为空!");
        }
    }

    /**
     * 设置是否添加提醒
     * @param isSubscription
     * @return
     */
    @RequestMapping(value = "/updateUserSubscription", method = RequestMethod.POST)
    public @ResponseBody
    String updateUserSubscription(@RequestParam(value = "userId", required = false) Integer userId,
                                  @RequestParam(value = "isSubscription", required = false) Byte isSubscription) {
        if (isSubscription != null && userId != null) {
            try {
                Map<String, Object> map = new HashMap<String, Object>();
                map.put("id", userId);
                map.put("isSubscription", isSubscription);
                this.iAdminsService.updatePriAdminSubscription(map);
                return JsonUtil.returnJsonSuccess("设置提醒成功!");
            } catch (Exception e) {
                logger.error("设置是否提醒异常!", e);
                return JsonUtil.returnJsonUnSuccess("设置是否提醒异常!");
            }
        } else {
            logger.error("设置提醒类型或用户编号不能为空!");
            return JsonUtil.returnJsonUnSuccess("设置提醒类型或用户编号不能为空!");
        }
    }

    /**
     * 设置是否添加提醒
     * @param isSubscription
     * @return
     */
    @RequestMapping(value = "/SelectUserSubscript", method = RequestMethod.POST)
    public @ResponseBody
    String selectUserSubscript(@RequestParam(value = "userId", required = false) Integer userId) {
        if (userId != null) {
            try {
                Map<String, Object> map = new HashMap<String, Object>();
                map.put("userId", userId);
                List<AdminSubscriberSettingDO> listAdminSubscriberSetting = this.iAdminsService
                    .selectUserSubscript(map);
                return JsonUtil.writeObjectNoPageJSON(listAdminSubscriberSetting);
            } catch (Exception e) {
                logger.error("查询用户提醒异常!", e);
                return JsonUtil.returnJsonUnSuccess("查询用户提醒异常!");
            }
        } else {
            logger.error("查询用户提醒设置时用户信息为空!");
            return JsonUtil.returnJsonUnSuccess("查询用户提醒设置时用户信息为空!");
        }
    }

    /**
     *添加提醒类型信息
     * @param isSubscription
     * @return
     */
    @RequestMapping(value = "/addUserSubscript", method = RequestMethod.POST)
    public @ResponseBody
    String addUserSubscript(@RequestParam(value = "userId", required = false) Integer userId,
                            @RequestParam(value = "subscript", required = false) String subscript) {
        try {
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("userId", userId);
            map.put("subscript", subscript.split("[,]"));
            this.iAdminsService.addUserSubscript(map);
            return JsonUtil.returnJsonSuccess("添加提醒类型成功!");
        } catch (Exception e) {
            logger.error("添加提醒类型异常!", e);
            return JsonUtil.returnJsonUnSuccess("添加提醒类型异常!");
        }
    }
}
